package com.lx.yygh.api;

import com.lx.yygh.common.helper.AuthContextHolder;
import com.lx.yygh.common.result.Result;
import com.lx.yygh.model.user.Patient;
import com.lx.yygh.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author c先生
 * @date 2022年10月25日 下午 7:16:56
 * @describe
 */
@Api(tags = "就诊人管理的接口")
@RestController
@RequestMapping("/api/patient")
public class PatientApiController {

    @Resource
    private PatientService patientService;

    @ApiOperation(value = "获取就诊人列表")
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request){
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findAll(userId);
        return Result.ok(list);
    }

    @ApiOperation(value = "根据id获取就诊人信息")
    @GetMapping("/auth/get/{id}")
    public Result getById(@PathVariable("id") Long id){
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }

    @ApiOperation(value = "保存就诊人信息")
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient,HttpServletRequest request){
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        Date birthdate = patient.getBirthdate();
        DateTime time = new DateTime(birthdate);
        DateTime time1 = time.plusDays(1);
        patient.setBirthdate(time1.toDate());
        boolean save = patientService.save(patient);
        if(save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    
    @ApiOperation(value = "更新就诊人信息")
    @PutMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient){
        Date birthdate = patient.getBirthdate();
        DateTime time = new DateTime(birthdate);
        DateTime time1 = time.plusDays(1);
        patient.setBirthdate(time1.toDate());
        boolean b = patientService.updateById(patient);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "删除就诊人信息")
    @DeleteMapping("/auth/remove/{id}")
    public Result deletePatient(@PathVariable("id")Long id){
        boolean b = patientService.removeById(id);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据id获取就诊人信息")
    @GetMapping("/inner/get/{id}")
    public Patient getPatientById(@PathVariable("id") Long id){
        Patient patient = patientService.getPatientById(id);
        return patient;
    }
}
